package com.warybee.mqspring.convert;/**
 * All rights Reserved, Designed By warybee
 *
 * @description:
 * @author: jeebee
 */

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.MessageConverter;

/**
 *@Description TODO
 *@author jeebee
 */
public class TextMessageConvert implements MessageConverter {


    /**
     * Convert a Java object to a Message.
     * @param o
     * @param messageProperties
     * @return
     * @throws MessageConversionException
     */
    @Override
    public Message toMessage(Object o, MessageProperties messageProperties) throws MessageConversionException {
        return new Message(o.toString().getBytes(),messageProperties);
    }

    /**
     * Convert from a Message to a Java object.
     * @param message
     * @return
     * @throws MessageConversionException
     */
    @Override
    public Object fromMessage(Message message) throws MessageConversionException {
        String contentType = message.getMessageProperties().getContentType();
        if (contentType!=null&&contentType.contains("text")){
            return new String(message.getBody());
        }
        return message.getBody();
    }
}
